<template>
    <div>
        <el-card>
            <template #header>
                <h3>权限管理</h3>
            </template>
            <!-- 表格区域 -->
            <el-table :data="permission">
                <el-table-column label="权限名称" prop="resident" align="center">
                    <template #default="scope">
                        {{ permissioName(scope.row.name) }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-switch :key="scope.row.name" v-model="scope.row.value" :active-value=1 :inactive-value=0
                            @click="updatePermission(scope.row)"></el-switch>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script>
import { permissionFindByIdAPI, permissionUpdateAPI } from '@/api/permission';
import { ElMessage } from 'element-plus';

export default {
    data() {
        return {
            user_id: this.$route.params.id,
            permission: []
        }
    },
    methods: {
        loadPermissionList() {
            permissionFindByIdAPI(this.$route.params.id).then((resp) => {
                if (resp.data.code == 2000) {
                    this.permission = resp.data.datas;
                } else {
                    ElMessage.error(resp.data.message);
                }
            })
        },
        permissioName(name) {
            switch (name) {
                case 'resident':
                    return '居民账号管理'
                case 'doctor':
                    return '医生账号管理'
                case 'admin':
                    return '管理员账号管理'
                case 'institution':
                    return '医疗机构信息管理'
                case 'medication_device':
                    return '医疗器械和药品信息管理'
                case 'type_info':
                    return '科室、职称、医疗机构类型管理'
                case 'appointment_admin':
                    return '预约挂号管理（管理员端）'
                case 'appointment_doctor':
                    return '预约挂号管理（医生端）'
                case 'role_manage':
                    return '角色与权限管理'
                case 'announcement':
                    return '公告管理'
                case 'opinion':
                    return '意见反馈'
                case 'evaluation':
                    return '患者评价'
                default:
                    return name
            }
        },
        updatePermission(row) {
            let params = {
                user_id: this.$route.params.id,
                name: row.name,
                value: row.value
            }
            permissionUpdateAPI(params).then((resp) => {
                if (resp.data.code == 2000) {
                    ElMessage.success(resp.data.message);
                } else {
                    ElMessage.error(resp.data.message);
                }
            })
        }
    },
    created() {
        this.loadPermissionList();
    }
}
</script>

<style scoped></style>